package com.example.statistics.ui.market

import android.os.Bundle
import com.example.statistics.R
import com.example.statistics.bean.MarketBean
import com.example.statistics.bean.MarketCityBean
import com.example.statistics.common.BaseLoadingListActivity
import com.example.statistics.ex.onActionLongClicked
import com.example.statistics.net.UrlConfig
import com.example.statistics.utils.Constants
import com.example.statistics.utils.activityFinish
import com.example.statistics.utils.activityForResult
import com.jhj.slimadapter.SlimAdapter
import com.jhj.slimadapter.holder.ViewInjector
import org.jetbrains.anko.startActivity

class MarketCityListActivity : BaseLoadingListActivity<MarketCityBean>() {

    var action: String? = ""

    override val title: String
        get() = "城市"

    override val url: String
        get() = UrlConfig.MARKET_CITY_LIST

    override val itemLayoutRes: Int
        get() = R.layout.list_item_single_display

    override val httpParams: MutableList<Pair<String, String>>
        get() {
            val params = mutableListOf<Pair<String, String>>()
            val city = intent.getStringExtra(Constants.CITY)
            params.add("city" to city.orEmpty())
            return params
        }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        action = intent.getStringExtra(Constants.ACTION)
        /*topBarRightImage {
            activityForResult<MarketCityAddActivity>() {
                refresh()
            }
        }*/
    }

    override fun itemViewConvert(adapter: SlimAdapter, injector: ViewInjector, bean: MarketCityBean, position: Int) {
        injector.text(R.id.text_view, bean.city)
            .clicked {
                if (action == Constants.SELECTOR) {
                    activityFinish(Constants.DATA to bean)
                }else{
                    activityForResult<MarketCustomerListActivity>(Constants.CITY to bean.city){
                        refresh()
                    }
                }
            }
            .onActionLongClicked(
                "市场" to {
                    activityForResult<MarketInfoListActivity>(Constants.CITY to bean.city){
                        refresh()
                    }
                },
                "客户" to {
                    activityForResult<MarketCustomerListActivity>(Constants.CITY to bean.city){
                        refresh()
                    }
                }
            )

    }

}
